//
//  YHHomeCell.swift
//  XLWB
//
//  Created by key on 16/6/27.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit

class YHHomeCell: UITableViewCell {

    //记录toolbar 的约束
    var const: Constraint?
    
    //MARK: - 数据传递
    var status: YHHomeList? {
        
        didSet{

            //原创
            originalView.status = status
            
            //评论 转发 赞 工具条
            toolbar.status = status
            
            
            //转发微博数据传递
            //卸载原有的toolbar的约束
            const?.uninstall()
            if status?.retweeted_status == nil {
                
                retweetView.hidden = true
                
                //重新对toolBar约束
//                //方式一
//                toolbar.snp_remakeConstraints(closure: { (make) -> Void in
//                    
//                    make.leading.trailing.equalTo(contentView)
//                    make.top.equalTo(originalView.snp_bottom)
//                    make.height.equalTo(35)
//                    
//                })
                
                //方式二
                toolbar.snp_updateConstraints(closure: { (make) -> Void in
                    
                    const = make.top.equalTo(originalView.snp_bottom).constraint
                    
                })
                
            }else {
                
                
                retweetView.status = status
                
                retweetView.hidden = false
                
                
                //重新对toolBar约束
                toolbar.snp_updateConstraints(closure: { (make) -> Void in
                    
                    const = make.top.equalTo(retweetView.snp_bottom).constraint
                    
                })
                
            }
            
            
        }
        
    }
    
    //MARK: - 懒加载子控件
    //原创微博
    lazy var originalView: YHOriginalView = {
        
        let originalV = YHOriginalView()
        
        
        originalV.backgroundColor = UIColor.whiteColor()
        return originalV
    
    }()
    
    //添加转发 评论 赞  工具条 toolbar
    lazy var toolbar: YHToolbar = {
       
        let toolB = YHToolbar()
        
        return toolB
  
    }()
    
    //转发微博View
    lazy var retweetView: YHRetweetView = {
        
       
        let retweetV = YHRetweetView()
        
        retweetV.backgroundColor = RGB(237, green: 237, blue: 237)
        
        return retweetV
        
    }()
    
    
    
    //MARK: - 入口
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        
        
        //加载子控件
        loadSubView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //MARK: - 加载子控件
    func loadSubView() {
        
        //添加原创微博View
        contentView.addSubview(originalView)
        //添加转发 评论 赞  工具条 toolbar
        contentView.addSubview(toolbar)
        //转发微博View
        contentView.addSubview(retweetView)
        
        //添加约束
        setSubViewFarme()
        
    }
    

    
    //MARK: - 添加子控件约束
    func setSubViewFarme() {
        
        //添加原创微博View
        originalView.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.equalTo(contentView)
            
            make.top.equalTo(contentView).offset(8)
            
        }
        
        
        //转发微博View
        retweetView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
            
        }
        
        //添加转发 评论 赞  工具条 toolbar
        toolbar.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.equalTo(contentView)
            const = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(35)
 
        }
        
        
        //约束cell contentView自身
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.trailing.equalTo(self)
            
            make.bottom.equalTo(toolbar)
            
        }
        
    }
    
}
